
import Vue from 'vue'
//导入vuex
import Vuex from 'vuex'

Vue.use(Vuex)

//导入localStorageTool中的方法
import {
    addLocalGoods,
    getTotalCount,
    updateLocalGoods,
    deleteLocalGoodsById,
    deleteLocalGoodsByIds
} from '../common/localStorageTool.js'

//全局数据存储
const store = new Vuex.Store({
    //存储数据
    state:{
        buycount:0
    },
    //获取数据
    getters:{
        getBuyCount(state){
            if(state.buycount>0){
                return state.buycount
            }else{
                return getTotalCount()
            }
        }
    },
    //操作数据, 增,删,改
    mutations:{
        addGoods(state,goodsObj){
            state.buycount = addLocalGoods(goodsObj)
        },
        updateGoods(state,changedGoods){
            //修改本地的数量, 统计总数量, 赋值给右上角购物车
            state.buycount = updateLocalGoods(changedGoods)
        },
        deleteGoodsById(state,goodsId){
            state.buycount = deleteLocalGoodsById(goodsId)
        },
        deleteGoodsByIds(state,goodsids){//约定，传递过来的goodsids是一个数组[87,92]
            //根据ids删除本地的商品
            state.buycount = deleteLocalGoodsByIds(goodsids)
        }
    }
})

export {store}